﻿using Microsoft.Win32;
using SharpShell.Attributes;
using SharpShell.Diagnostics;
using SharpShell.Registry;
using SharpShell.ServerRegistration;
using SharpShell.ServiceRegistry;
using SharpShell.SharpIconHandler;
using SharpShell.SharpInfoTipHandler;
using SharpShell.SharpThumbnailHandler;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace Tisn
{
    [ComVisible(true)]
    [COMServerAssociation(AssociationType.ClassOfExtension, ".cdr")]
    [Guid("1ABA3AF4-E3DE-4594-96F3-8B20A49FC5FA")]
    public class CorelDRAWFileInfoTip : SharpInfoTipHandler
    {/// <summary>
     /// 计算文件大小函数(保留两位小数),Size为字节大小
     /// </summary>
     /// <param name="Size">初始文件大小</param>
     /// <returns></returns>
        string CountSize(long Size)
        {
            string m_strSize = "";
            long FactSize = 0;
            FactSize = Size;
            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString("F2") + " 字节";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " KB";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " MB";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " GB";
            return m_strSize;
        }
        protected override string GetInfo(RequestedInfoType infoType, bool singleLine)
        {
            switch (infoType)
            {
                case RequestedInfoType.InfoTip:
                    if (singleLine)
                    {
                        return string.Format("{0}", Path.GetFileName(SelectedItemPath));
                    }
                    else
                    {
                        FileInfo fileInfo = new FileInfo(SelectedItemPath);
                        CDRFile file = new CDRFile(SelectedItemPath);
                        return $@"{Path.GetFileName(SelectedItemPath)}
类型:CorelDRAW Graphic
大小:{CountSize(fileInfo.Length)}
修改时间:{fileInfo.LastWriteTime.ToString()}
版本:{file.GetVersionString()}";
                    }
                case RequestedInfoType.Name:
                    return string.Format("{0}", Path.GetFileName(SelectedItemPath));
                default:
                    return string.Format("{0}", Path.GetFileName(SelectedItemPath));
            }
        }

        [PrevCustomRegisterFunction]
        internal static void P(Type type, RegistrationType registrationType)
        {
            Utility.PrevReg(type, registrationType, Utility.CDR_Names);
        }
    }

    [ComVisible(true)]
    [COMServerAssociation(AssociationType.ClassOfExtension, ".cdr", ".cdt", ".cmx")]
    [Guid("F939DADC-5309-4678-B1AC-12A9A432FF7D")]
    public class CorelDRAWFileThumbnail : SharpThumbnailHandler
    {
        protected override Bitmap GetThumbnailImage(uint width)
        {
            CDRFile file = new CDRFile(SelectedItemStream);
            return file.GetThumbnail(width);
        }
        [PrevCustomRegisterFunction]
        internal static void P(Type type, RegistrationType registrationType)
        {
            Utility.PrevReg(type, registrationType, Utility.CDR_Names);
        }
    }

    [ComVisible(true)]
    [COMServerAssociation(AssociationType.ClassOfExtension, ".cdr", ".cdt", ".cmx")]
    [Guid("58A45394-4F2E-46B8-B36D-E9FB60C64C4B")]
    public class CorelDRAWFileCDRIcon : SharpIconHandler
    {
        protected override Icon GetIcon(bool smallIcon, uint iconSize)
        {
            var ext = Path.GetExtension(SelectedItemPath);
            switch (ext)
            {
                case ".cdr":
                    return Icon.FromHandle(Utility.ResizeBitmap(Resource.icon_cdr, (int)iconSize).GetHicon());
                case ".cdt":
                    return Icon.FromHandle(Utility.ResizeBitmap(Resource.icon_cdt, (int)iconSize).GetHicon());
                case ".cmx":
                    return Icon.FromHandle(Utility.ResizeBitmap(Resource.icon_cmx, (int)iconSize).GetHicon());
            }
            return Icon.FromHandle(Utility.ResizeBitmap(Resource.icon_cdr, (int)iconSize).GetHicon());
        }
        [PrevCustomRegisterFunction]
        internal static void P(Type type, RegistrationType registrationType)
        {
            Utility.PrevReg(type, registrationType, Utility.CDR_Names);
        }
    }
}
